package org.dding.msc.service;

import java.util.concurrent.TimeUnit;

import org.dding.msc.service.impl.ServiceContainerImpl;

/**
 * The factory class for service containers.
 */
public class MSCFactory {

	private MSCFactory() {
	}

	/**
	 * Create a new instance with a generated name and default thread pool.
	 * 
	 * @return a new service container instance
	 */
	public static ServiceContainer create() {
		int cpuCount = Runtime.getRuntime().availableProcessors();
		int coreSize = Math.max(cpuCount << 1, 2);
		return new ServiceContainerImpl(null, coreSize, 30L, TimeUnit.SECONDS);
	}

	/**
	 * Create a new instance with a given name and default thread pool.
	 * 
	 * @param name
	 *            the name of the new container
	 * @return a new service container instance
	 */
	public static ServiceContainer create(String name) {
		int cpuCount = Runtime.getRuntime().availableProcessors();
		int coreSize = Math.max(cpuCount << 1, 2);
		return new ServiceContainerImpl(name, coreSize, 30L, TimeUnit.SECONDS);
	}

	/**
	 * Create a new instance with a generated name and specified initial thread
	 * pool settings.
	 * 
	 * 
	 * @param coreSize
	 *            the core pool size (must be greater than zero)
	 * @param keepAliveTime
	 *            the amount of time that non-core threads should linger without
	 *            tasks
	 * @param keepAliveTimeUnit
	 *            the time unit for {@code keepAliveTime}
	 * @return a new service container instance
	 */
	public static ServiceContainer create(int coreSize, long keepAliveTime,
			TimeUnit keepAliveTimeUnit) {
		return new ServiceContainerImpl(null, coreSize, keepAliveTime,
				keepAliveTimeUnit);
	}

	/**
	 * Create a new instance with a given name and specified initial thread pool
	 * settings.
	 * 
	 * 
	 * @param name
	 *            the name of the new container
	 * @param coreSize
	 *            the core pool size (must be greater than zero)
	 * @param keepAliveTime
	 *            the amount of time that non-core threads should linger without
	 *            tasks
	 * @param keepAliveTimeUnit
	 *            the time unit for {@code keepAliveTime}
	 * @return a new service container instance
	 */
	public static ServiceContainer create(String name, int coreSize,
			long keepAliveTime, TimeUnit keepAliveTimeUnit) {
		return new ServiceContainerImpl(name, coreSize, keepAliveTime,
				keepAliveTimeUnit);
	}
}